package com.yuandengta.common.valid;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.HashSet;
import java.util.Set;

/**
 * Integer Value 值校验器 {@link ListValueConstraintValidator}
 *
 * @author Kevin
 * @email: 178676392@qq.com
 */
public class ListValueConstraintValidator implements ConstraintValidator<ListValue, Integer> {
    private Set<Integer> integers = new HashSet<>();

    /**
     * 初始化，获取 ListValue 注解中的 values 值
     * @param constraintAnnotation
     */
    @Override
    public void initialize(ListValue constraintAnnotation) {
        int[] values = constraintAnnotation.values();
        for (int value : values) {
            integers.add(value);
        }
    }

    /**
     * 判断是否校验成功
     * @param value 需要校验的值，传入的校验参数值
     * @param context
     * @return
     */
    @Override
    public boolean isValid(Integer value, ConstraintValidatorContext context) {
        return integers.contains(value);
    }
}
